{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周学习内容\n",
    "* A.列表的基本方法\n",
    "* B.列表的高级方法_copy()\n",
    "* C.列表的高级方法_枚举\n",
    "\n",
    "## 本周学习内容\n",
    "* A.知识点_课本练习1_list\n",
    "* B.知识点_课本练习1_切片\n",
    "* C.知识点_元组\n",
    "* D.知识点_课堂练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课本练习1\n",
    "* 目标： “Don't panic” ==> \"on tap\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 方法一： 使用list的方法完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "# 1.删除后面四个元素 a n i c\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "# 2. 删除 D  ‘\n",
    "plist.remove('D')\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# 3. 数学案例： 计算 （(a+b)*(c+d)）\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#['o','n','t',' '][ a    ,   p     ]\n",
    "print(plist)\n",
    "\n",
    "# 4. 数据插入  实现：’t‘ 和’ ‘ 的位置转换\n",
    "# plist.insert(2,plist.pop(3))\n",
    "# print(plist)\n",
    "# print(plist.pop(2))  # 1，代表删除的内容的值 2，他删除了plist的这个值\n",
    "plist.insert(3,plist.pop(2))\n",
    "print(plist)\n",
    "# 5. list to str\n",
    "new_phrase = ''.join(plist)\n",
    "\n",
    "\n",
    "\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "\n",
    "# plist 改变了原先的状态么？ 列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，请先用copy()复制数据\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 方法二： 使用切片的方法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组\n",
    "> 元组使用小括号 ( )，列表使用方括号 [ ]；  \n",
    "> 元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可;  \n",
    "> 元组与列表类似，但不同之处在于元组的元素，创建后便不能再进行修改。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "(1, 2, 3, 4, 5)\n",
      "('a', 'b', 'c', 'd')\n"
     ]
    }
   ],
   "source": [
    "tup0 = ()\n",
    "tup1 = (1, 2, 3, 4, 5 )\n",
    "tup2 = \"a\", \"b\", \"c\", \"d\"\n",
    "print(tup0)\n",
    "print(tup1)\n",
    "print(tup2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 访问元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "('a', 'b')\n"
     ]
    }
   ],
   "source": [
    "tup1 = (1, 2, 3, 4, 5 )\n",
    "tup2 = \"a\", \"b\", \"c\", \"d\"\n",
    "\n",
    "print (tup1[2])\n",
    "print (tup2[0:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 修改元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5, 'a', 'b', 'c', 'd')\n"
     ]
    }
   ],
   "source": [
    "tup1 = (1, 2, 3, 4, 5 )\n",
    "tup2 = \"a\", \"b\", \"c\", \"d\"\n",
    "\n",
    "tup3 = tup1 + tup2\n",
    "print (tup3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 删除元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "tup1 = (1, 2, 3, 4, 5 )\n",
    "print (tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'tup' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-228bcebcd3ca>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mdel\u001b[0m \u001b[0mtup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'tup' is not defined"
     ]
    }
   ],
   "source": [
    "del tup\n",
    "print (tup) #元组已被删除，因此报错。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课堂练习（购物车项目）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 购物车程序项目：(20分钟)\n",
    "# 要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 　　  2、让用户输入商品编号进行商品的购买。\n",
    "# 　　  3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "# 　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：500\n",
      "------goods list-------\n",
      "['10001', 'python课本', '88']\n",
      "['10002', '蜜雪冰城—冰茶', '10']\n",
      "['10003', 'iphone13 pro', '9999']\n",
      "['10004', '华为电-跑车', '400000']\n",
      "['10005', '小米手环', '199']\n",
      "\n",
      "请输入商品的ID：10001\n",
      "['10001', 'python课本', '88']\n",
      "[['10001', 'python课本', '88']]\n"
     ]
    }
   ],
   "source": [
    "goods = [\n",
    "    [\"10001\",\"python课本\",\"88\"],\n",
    "    [\"10002\",\"蜜雪冰城—冰茶\",\"10\"],\n",
    "    [\"10003\",\"iphone13 pro\",\"9999\"],\n",
    "    [\"10004\",\"华为电-跑车\",\"400000\"],\n",
    "    [\"10005\",\"小米手环\",\"199\"]\n",
    "]\n",
    "user_shop_list = []\n",
    "\n",
    "支付宝余额 = input(\"请输入您的余额：\")\n",
    "info = '''------goods list-------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(goods[0],goods[1],goods[2],goods[3],goods[4])\n",
    "print(info)\n",
    "user_input_ID = input(\"请输入商品的ID：\")\n",
    "for good in goods:\n",
    "    print(good)\n",
    "    # 判断商品的id是否正确\n",
    "    if user_input_ID == good[0]:\n",
    "        # 判断用户的余额是否够用\n",
    "        if int(good[2]) < int(支付宝余额):\n",
    "            # 把商品加进list\n",
    "            user_shop_list.append(good)\n",
    "            # 扣除支付宝余额\n",
    "            break\n",
    "\n",
    "    else:\n",
    "        print(\"您输入的商品信息有问题，请输入正确的商品ID\")\n",
    "\n",
    "print(user_shop_list)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
